package heima_bixiangdong.pro_con;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// Condition实例代码，再写一遍，加深印象
class BoundBuffer{
    private final Lock lock = new ReentrantLock();
    private final Condition notFull = lock.newCondition();
    private final Condition notEmpty = lock.newCondition();
    private final Object[] items = new Object[20];
    int putptr,takeptr,count;
    public void put(Object x) throws InterruptedException {
        lock.lock();
        try {
            while (count == items.length){
                notFull.await();
            }
            items[putptr] = x;
            if(++putptr == items.length){
                putptr = 0;
            }
            ++count;
            notEmpty.signal();
        }finally {
            lock.unlock();
        }
    }
    public Object take() throws InterruptedException{
        lock.lock();
        try {
            while (count == 0){
                notEmpty.await();
            }
            Object x = items[takeptr];
            if(++takeptr == items.length){
                takeptr = 0;
            }
            --count;
            notFull.signal();
            //return x;
            return x+""+(takeptr+1);
        }finally {
            lock.unlock();
        }
    }
}
public class LockDemo3 {

    public static void main(String[] args) {
        BoundBuffer boundBuffer = new BoundBuffer();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        boundBuffer.put(Thread.currentThread().getName()+"馒头");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"Producer-A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        boundBuffer.put(Thread.currentThread().getName()+"馒头");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"Producer-B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Object take = boundBuffer.take();
                        System.out.println(Thread.currentThread().getName()+"消费"+take);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"Consumer-A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Object take = boundBuffer.take();
                        System.out.println(Thread.currentThread().getName()+"消费"+take);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"Consumer-B").start();
    }
}
